import jieba
# 打开并读取“西游记.txt”
txt = open(r"其他练习\斗罗大陆\斗.罗.大.陆.txt", "rb").read()
# 构建排除词库
excludes = {"自己", "已经", "没有", "他们", "一个", "身体", "我们",
              "就是", "学院","虽然", "大师", "武魂", "身上", "不是", "知道", "此时",
              "什么", "时候", "能够", "魂力", "魂师", "同时", "这个", "之中", "只是", 
              "如果", "实力", "出现", "你们", "出现", "魂环", "现在", "还是", "那么", "有些",
              "感觉", "攻击", "斗罗", "可以", "起来", "看到", "帝国", "昊天", "这样", "因为",
              "完全", "可能", "光芒", "一般", "看着", "只有", "心中", "魂技", "怎么", "对手",
              "变得", "竟然", "不会", "这里", "时间", "开始", "一下", "这些", "领域", "一声",
              "无法", "突然", "释放"}
# 使用jieba分词
words = jieba.lcut(txt)
# 对划分的单词计数
counts = {}
for word in words:
    if len(word) == 1:
        continue
    elif word == "千手修罗" or word == "海神" or word == "修罗神" or word == "小三" or word == "三哥" or word == "小怪物" or word == "唐银" or word == "蓝昊王" or word == "老师":
        rword = "唐三"
    elif word == "小舞姐":
        rword = "小舞"
    elif word == "沐白" or word == "戴老大" or word == "戴少" or word == "白虎斗罗" or word == "战神":
        rword = "戴沐白"
    elif word == "小魔女" or word == "七宝琉璃" or word == "九彩斗罗" or word == "九宝斗罗" or word == "大陆第一辅助系魂师":
        rword = "宁荣荣"
    elif word == "小奥" or word == "大香肠叔叔" or word == "香肠专卖" or word == "食神斗罗" or word == "食神":
        rword = "奥斯卡"
    elif word == "胖子" or word == "小胖" or word == "傻胖" or word == "凤凰斗罗" or word == "凤凰之神":
        rword = "马红俊"
    elif word == "竹清" or word == "七妹" or word == "幽冥灵猫" or word == "幽冥斗罗" or word == "速度之神":
        rword = "朱竹清"
    else:
        rword = word
    counts[rword] = counts.get(rword, 0) + 1
# 删除无意义的词语
for word in excludes:
    del counts[word]
# 按词语出现的次数排序
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
# 采用固定的格式进行输出
for i in range(9):
    word, count = items[i]
    print("{0:<5}{1:>5}次".format(word, count))

    # "弗兰德", "就算", "金色", "修炼", "情况", "顿时", "整个",
    #           "瞬间", "似乎", "还有", "封号", "如何", "任何", "千仞", "出来", "能量", "自然",
    #           "巨大", "技能", "强大"
